# load libraries
library(patchwork)
library(sf)
library(tidyverse)

# load data
AdminData <- read.csv("BSW/PreparedData.csv")
ShapeFileStates <- st_read("BSW/ShapeFiles/vg5000_ebenen_1231/VG5000_LAN.shp") %>%
  filter(GF == 9)
ShapeFileLKCity <- st_read("BSW/ShapeFiles/vg5000_ebenen_1231/VG5000_KRS.shp") %>% 
  mutate(AGS_0 = as.numeric(AGS_0))
ShapeFileVGs <- st_read("BSW/ShapeFiles/vg5000_ebenen_1231/VG5000_VWG.shp") %>% 
  mutate(ARS_0 = as.numeric(ARS_0))
ShapeFileMunicip <- st_read("BSW/ShapeFiles/vg5000_ebenen_1231/VG5000_GEM.shp") %>% 
  mutate(AGS_0 = as.numeric(AGS_0))

# merge shape files and admin data for German Map

# subset data to Landkreis + kreisfreie Stadt
LKData <- AdminData %>%
  filter(Verwaltungstyp == "Landkreis")

# merge data together
MergedAllLKs <- right_join(LKData, ShapeFileLKCity,
  by = c("GKZ" = "AGS_0")
)
MergedLKs <- left_join(LKData, ShapeFileLKCity,
  by = c("GKZ" = "AGS_0")
)

# plot map

# BSW in Counties across whole Germany
ggplot() +
  geom_sf(
    data = MergedAllLKs,
    aes(geometry = geometry),
    fill = NA, linewidth = 0.2
  ) +
  geom_sf(
    data = MergedLKs,
    aes(
      geometry = geometry,
      fill = bsw_option,
    ),
    color = "black", linewidth = 0.2,
  ) +
    geom_sf(
    data = ShapeFileStates,
    aes(geometry = geometry),
    fill = NA, linewidth = 1, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation",
  ) +
  theme_void()


# merge shape files and admin Data for State Maps

# subset data to Municipalities
MunicipalData <- AdminData %>%
  filter(Verwaltungstyp == "Gemeinde")

# merge data together

# VGs
MergedAllVGs <- right_join(MunicipalData, ShapeFileVGs,
  by = c("ARS" = "ARS_0")
)
MergedVGs <- left_join(MunicipalData, ShapeFileVGs,
  by = c("ARS" = "ARS_0")
)

# Municipalities
MergedAllMunicips <- right_join(MunicipalData, ShapeFileMunicip,
  by = c("GKZ" = "AGS_0")
)
MergedMunicips <- left_join(MunicipalData, ShapeFileMunicip,
  by = c("GKZ" = "AGS_0")
)

# bind data on VGs and municipalities together +
# keep only observed data
MergedAllVGsMunicips <- bind_rows(MergedAllVGs, MergedAllMunicips)
SampleVGsMunicips <- MergedAllVGsMunicips %>%
  drop_na(ShareCDU24)

# BSW Municipality Level

#  Saarland
SaarlandPlot <- ggplot() +
  geom_sf(
    data = SampleVGsMunicips %>%
      filter(Bundesland == "Saarland"),
    aes(
      geometry = geometry,
      fill = bsw_option
    ),
    color = "black", linewidth = 0.4,
  ) +
  geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Saarland"),
    aes(geometry = geometry),
    fill = NA, linewidth = 1.3, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation"
  ) +
  theme_void() +
  theme(legend.position = "right")
SaarlandPlot

# Rhineland-Palatinate
RhinelandPalatinatePlot <- ggplot() +
  geom_sf(
    data = SampleVGsMunicips %>%
      filter(Bundesland == "Rheinland-Pfalz"),
    aes(
      geometry = geometry,
      fill = bsw_option,
    ),
    color = "black", linewidth = 0.4
  ) +
  geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Rheinland-Pfalz" & Verwaltungstyp == "kreisfreie Stadt"),
    aes(geometry = geometry,
    fill = factor(bsw_option)
    ),
    color = "black", linewidth = 0.4
  ) +
    geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Rheinland-Pfalz"),
    aes(geometry = geometry),
    fill = NA, linewidth = 1.3, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation"
  ) +
  theme_void() +
  theme(legend.position = "none")
RhinelandPalatinatePlot


# Thuringia
ThuringiaPlot <- ggplot() +
  geom_sf(
    data = SampleVGsMunicips %>%
      filter(Bundesland == "Thüringen"),
    aes(
      geometry = geometry,
      fill = factor(bsw_option),
    ),
    color = "black", linewidth = 0.4) +
  geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Thüringen" & Verwaltungstyp == "kreisfreie Stadt"),
    aes(geometry = geometry,
    fill = factor(bsw_option)
    ),
    color = "black", linewidth = 0.4
  ) +
    geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Thüringen"),
    aes(geometry = geometry),
    fill = NA, linewidth = 1.3, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation"
  ) +
  theme_void() +
  theme(legend.position = "none")
ThuringiaPlot

# Saxony
SaxonyPlot <- ggplot() +
  geom_sf(
    data = SampleVGsMunicips %>%
      filter(Bundesland == "Sachsen"),
    aes(
      geometry = geometry,
      fill = factor(bsw_option),
    ),
    color = "black", linewidth = 0.4
  ) +
  geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Sachsen" & Verwaltungstyp == "kreisfreie Stadt"),
    aes(geometry = geometry,
    fill = factor(bsw_option)
    ),
    color = "black", linewidth = 0.4
  ) +
    geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Sachsen"),
    aes(geometry = geometry),
    fill = NA, linewidth = 1.3, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation"
  ) +
  theme_void() +
  theme(legend.position = "none")
SaxonyPlot

# BSW Municipalities - Mecklenburg-Western Pomerania
MecklenburgPlot <- ggplot() +
  geom_sf(
    data = SampleVGsMunicips %>%
      filter(Bundesland == "Mecklenburg-Vorpommern"),
    aes(
      geometry = geometry,
      fill = factor(bsw_option),
    ),
    color = "black", linewidth = 0.4
  ) +
  geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Mecklenburg-Vorpommern" & Verwaltungstyp == "kreisfreie Stadt"),
    aes(geometry = geometry,
    fill = factor(bsw_option)
    ),
    color = "black", linewidth = 0.4
  ) +
    geom_sf(
    data = MergedLKs %>% 
      filter(Bundesland == "Mecklenburg-Vorpommern"),
    aes(geometry = geometry),
    fill = NA, linewidth = 1.3, color = "black"
  ) +
  scale_fill_manual(values = c("grey", "#7c254f")) +
  labs(
    fill = "BSW Participation"
  ) +
  theme_void() +
  theme(legend.position = "none")



# combine the maps into a single plot
RhinelandPalatinatePlot +
  ggtitle("Rhineland-Palatinate") +
  ThuringiaPlot +
  ggtitle("Thuringia") +
  SaxonyPlot +
  ggtitle("Saxony") +
  MecklenburgPlot +
  ggtitle("Mecklenburg-Western Pomerania") +
  SaarlandPlot +
  ggtitle("Saarland") +
  plot_layout(ncol = 2,
              heights = c(1.15, 1.15, 0.7),
              widths = c(1, 1))

